﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class JCCD_Edit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblTitle.Text = "修改";
                this.tbRQ.Attributes.Add("onclick", "show()");
                initValue();
            }
        }

        private void initValue()
        {
            string id = Request.QueryString["id"];
            JCCD_Dao dao = new JCCD_Dao();
            JCCD jccd = dao.Query(id);
            this.hdId.Value = jccd.ID.ToString();

            // 部门
            Departments_Dao daoDep = new Departments_Dao();
            List<Department> lsDep = daoDep.QueryAll();
            this.ddlBM.DataSource = lsDep;
            this.ddlBM.DataTextField = "Name";
            this.ddlBM.DataValueField = "ID";
            this.ddlBM.DataBind();
            this.ddlBM.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem li = this.ddlBM.Items.FindByValue(jccd.BMID.ToString());
            if (li != null)
            {
                li.Selected = true;
            }

            // 人员
            InitRY();
            this.ddlRY.Items.FindByValue(jccd.RYID.ToString()).Selected = true;
            this.ddlCGY.Items.FindByValue(jccd.CGYID.ToString()).Selected = true;

            // 原因
            CS_JCCYYSet_Dao daoYY = new CS_JCCYYSet_Dao();
            List<CS_JCCYY> lsYY = daoYY.QueryAll();
            this.ddlYY.DataSource = lsYY;
            this.ddlYY.DataTextField = "MC";
            this.ddlYY.DataValueField = "BH";
            this.ddlYY.DataBind();
            this.ddlYY.Items.Insert(0, new ListItem("请选择", "0"));
            ListItem liYY = this.ddlYY.Items.FindByValue(jccd.JCCYYBH);
            if (liYY != null)
            {
                liYY.Selected = true;
            }

            this.hdOp.Value = jccd.LX.Trim();
            this.tbBH.Text = jccd.BH.Trim();
            this.tbRQ.Text = jccd.RQ.ToString("yyyy-MM-dd HH:mm:ss");
            this.tbGLDJ.Text = jccd.GLDJID.Trim();

            // this.tbYY.Text = jccd.YY; TODO fuck
        }

 

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
        }

        protected void ddlBM_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitRY();
        }

        private void InitRY()
        {
            this.ddlRY.Items.Clear();
            this.ddlCGY.Items.Clear();
            string depId = ddlBM.SelectedValue;
            if (depId != "0")
            {
                Guid guid = Guid.Parse(depId);
                using (var context = new Dao.ErpEntities())
                {
                    var query = from d in context.UserInDepartmentSet
                                join u in context.UserSet
                                on d.UserId equals u.Id
                                where d.DepartmentId == guid
                                select new
                                {
                                    Id = d.UserId,
                                    Name = u.Name
                                };
                    List<UserNames> list = new List<UserNames>();
                    if (query.Count() > 0)
                    {
                        foreach (var q in query)
                        {
                            UserNames un = new UserNames();
                            un.Id = q.Id.ToString();
                            un.Name = q.Name;
                            list.Add(un);
                        }
                    }

                    this.ddlRY.DataSource = list;
                    this.ddlRY.DataTextField = "Name";
                    this.ddlRY.DataValueField = "Id";
                    this.ddlRY.DataBind();

                    this.ddlCGY.DataSource = list;
                    this.ddlCGY.DataTextField = "Name";
                    this.ddlCGY.DataValueField = "Id";
                    this.ddlCGY.DataBind();
                }
            }

            this.ddlCGY.Items.Insert(0, new ListItem("请选择", "0"));
            this.ddlRY.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected class UserNames
        {
            public string Id { get; set; }
            public string Name { get; set; }
        }

        protected void btnSave0_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (this.ddlBM.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择部门";
                return;
            }

            if (this.ddlRY.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择人员";
                return;
            }
            if (this.ddlCGY.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择采购员";
                return;
            }
            if (this.ddlYY.SelectedValue.Equals("0"))
            {
                this.lbResult.Text = "请选择原因";
                return;
            }

            JCCD entity = new JCCD()
            {
                ID = Guid.Parse(this.hdId.Value),
                BH = this.tbBH.Text.Trim(),
                LX = this.hdOp.Value,
                BMID = Guid.Parse(this.ddlBM.SelectedValue),
                RYID = Guid.Parse(this.ddlRY.SelectedValue),
                RQ = DateTime.Parse(this.tbRQ.Text.Trim()),
                JCCYYBH = this.ddlYY.SelectedValue,
                GLDJID = this.tbGLDJ.Text.Trim()
            };

            if (this.ddlCGY.SelectedValue != "0")
            {
                entity.CGYID = Guid.Parse(this.ddlCGY.SelectedValue);
            }

            string resultMessage = "";
            try
            {
                JCCD_Dao dao = new JCCD_Dao();
                int result = dao.Update(entity);

                if (result == 1)
                {
                    resultMessage = "保存成功";
                    ClearControlsValue();
                }
            }
            catch (Exception ex)
            {
                resultMessage = "保存失败" + ex.Message;
            }
            finally
            {
                this.lbResult.Text = resultMessage;
            }
        }

        protected void btnClear_Click(object sender, ImageClickEventArgs e)
        {
            ClearControlsValue();
        }

        protected void btnReturn_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/JCCD_Query.aspx?op="+this.hdOp.Value);
        }
    }
}